var dataTable;
var last_column = 7;
var gritter_time = 2000;
var status = '';
var message = '';
var ischecked = false;

function initializeDatatable() {
    
    // datatable ajax source
    var ajaxSource = base_url + 'teams/contents/status/all';
    
    // edit datatable columns
    var aoColumns = [
        {'bSearchable': false, 'bVisible': true, 'bSortable': false, 'sClass': 'align-center'},  // checkbox
        {'bSearchable': false, 'bVisible': true, 'bSortable': true, 'sClass': 'align-left'},     // name
        {'bSearchable': true, 'bVisible': true, 'bSortable': true, 'sClass': 'hidden-480'},      // description
        {'bSearchable': true, 'bVisible': true, 'bSortable': true, 'sClass': 'hidden-480'},      // date_created
        {'bSearchable': true, 'bVisible': true, 'bSortable': true, 'sClass': 'hidden-480'},      // date_modified
        {'bSearchable': true, 'bVisible': true, 'bSortable': true, 'sClass': 'align-center'},    // status
        {'bSearchable': false, 'bVisible': true, 'bSortable': false, 'sClass': 'align-center'}   // options
    ];
    
    var filter = [
        null,
        {type:"text"},
        {type:"text"},
        {type:"text"},
        {type:"text"},
        {
            type:"select",
            bCaseSensitive: !0,
            values: ["active","inactive","archived"]
        },
        null
    ];

    // create datatable
    toDataTable('teams-table', ajaxSource, aoColumns, filter);
}

function addEventListeners()
{
    $('.btn-preview').off();
    $('.btn-delete').off();
    $('.btn-delete-multiple').off();
    
    $('.btn-delete').click(function(e) {
        e.preventDefault();
        var deleteLink = e.target.nodeName === 'A' ? e.target.href : $(e.target).closest('a').attr('href');
        $('#modal-btn-ok').attr('href', deleteLink);
        $('#modal').modal('show');
    });
    
    $('.btn-delete-multiple').click(function() {
        
        var selectedItems = [];
        $(":checkbox.record:checked").each(function(e) {
            var value = $(this).val();
            selectedItems.push(value);
        });
        
        if(selectedItems.length > 0) {
            
            if( selectedItems.length === 1 ) {
                
                $('#modal-title').text('Delete Record');
                $('#modal-body').html(deleteRecord);
            } else {
                $('#modal-title').text('Delete Records');
                $('#modal-body').html(deleteRecords);
            }
            
            $('#modal-btn-ok').attr('href', '#');
            $('#modal').modal('show');
            
        } else {
            alert('sdfsdf');
        }
        
    });
    
    $('.check-all, .record').change(function() {

        var selectedItems = [];
        $(":checkbox.record:checked").each(function(e) {
            var value = $(this).val();
            selectedItems.push(value);
        });
        
        if(selectedItems.length > 0) {
            $('.btn-delete-multiple').removeAttr('disabled');
        } else {
            $('.btn-delete-multiple').attr('disabled', true);
        }
    });
    
    // add event listener for check-all checkboxes
    checkAllEventHandler();
    
}

$(document).ready(function() {
    initializeDatatable();
});
